//
//  Created by David "Nirth" Sergey on 2007-09-25.
//  Copyright (c) 2007 Kiichigo. All rights reserved.
//
package kii.net
{
	import flash.net.SharedObject;
	import flash.utils.Dictionary;
	import flash.utils.Proxy;
	import flash.utils.flash_proxy;
	use namespace flash_proxy;
/**
 * Incapsulates SharedObject class for easier use.
 * @author  David "Nirth" Sergey
 */
public dynamic class SO extends Proxy
{
	/**
	 * @private
	 */
	private static var instances:Dictionary;
	/**
	 * Multiton pattern Factory Method implementation, returns unique instance of <code>SO</code> object per <code>id</code>. 
	 * @param id     Unique id.
	 * @param path   SharedObject path, see <code>SharedObject</code> documentation.
	 * @return       Either new instance if uniquee <code>id</code> provided, or already created instance otherwise.
	 */
	public static function get(id:String, path:String = '/'):SO
	{
		if(instances == null)
			instances = new Dictionary(true);
		
		if(instances[id] == null)
			instances[id] = new SO(id, path);
		
		return instances[id];
	}
	/**
	 * @private
	 */
	private var sharedObject:SharedObject;
	/**
	 * @private
	 */
	private var _path:String = null;
	/**
	 * Constructor, should not be used directly, use <code>SO.getSO(id:String)</code> method instead.
	 * @private
	 */
	public function SO(id:String = '', path:String = null){
		if(id != '')
		{
			_path = path;
			this.id = id;	
		}
	}
	/**
	 * Indicates current <code>SharedObject</code> local id.
	 */
	public function set id(value:String):void
	{
		sharedObject = SharedObject.getLocal(value, _path);
		_id = value;
	}
	public function get id():String
	{
		return _id;
	}
	/**
	 * @private
	 */
	private var _id:String;
	/**
	 * Clears LSO
	 */
	public function clear():void
	{
		if(sharedObject == null && _id == '')
			return;
		
		sharedObject.clear();
	}
	
	public function save():void
	{
		sharedObject.flush();
	}
	/**
	 * Saves data to ICookie.
	 * @param	key		data's key by which user can retrieve data later.
	 * @param	value	value which has to be stored.
	 */
	public function storeData(key:*, value:*):void
	{
		sharedObject.data[key] = value;
	}
	/**
	 * Retrieves previously saved data from ICookie.
	 * @param	key		data's key.
	 */
	public function retrieveData(key:*):*
	{
		return sharedObject.data[key];
	}
	/**
	 * Checks whenever ICookie has data with for provided <code>key</code>.
	 * @param	key		key to check.
	 */
	public function hasData(key:*):Boolean
	{
		if(sharedObject.data[key] == null)
			return false;
		return true;
	}
	/*******************
	 * Private Methods *
	 *******************/
	/**
	 * @private
	 */
	//ToDo: revome this and replace it with describeType implementation.
	//private var properties:Array = ['id', '_id', 'hasData', 'soInstance', 'retrieveData', 'storeData', 'save'];
	/**
	 * @private
	 */
	flash_proxy override function getProperty(name:*):*
	{
		//if(properties.indexOf(name) >= 0)
		//	return this[name];
		return sharedObject.data[name];
	}
	/**
	 * @private
	 */	
	flash_proxy override function setProperty(name:*, value:*):void
	{
		//if(properties.indexOf(name) >= 0)
		//{
		//	this[name] = value;
		//	return;
		//}
		sharedObject.data[name] = value;
	}
}

}